package com.example.yls.demoa;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class EditActivity extends AppCompatActivity {

    private EditText edtNo;
    private EditText edtName;
    private EditText edtAge;
    private Button btnSave;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit);
        setTitle("编辑学生");

        edtNo = findViewById(R.id.edt_stuNo);
        edtName = findViewById(R.id.edt_Name);
        edtAge = findViewById(R.id.edt_stuAge);
        btnSave = findViewById(R.id.btn_save);

        //取出MainActivity传过来的数据
        String stuno = getIntent().getStringExtra("STUNO");
        String name = getIntent().getStringExtra("NAME");
        int age = getIntent().getIntExtra("AGE",0);
        //显示出来
        edtNo.setText(stuno);
        edtName.setText(name);
        edtAge.setText(String.valueOf(age));

        //将修改完的数据传回到MainActivity中去
        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra("STUNO",edtNo.getText().toString());
                intent.putExtra("NAME",edtName.getText().toString());
                intent.putExtra("AGE",Integer.valueOf(edtAge.getText().toString()));
                setResult(4001,intent);
                finish();
            }
        });
    }
}
